package com.weizai.carshop.controller;


import cn.hutool.core.bean.BeanUtil;
import com.weizai.carshop.common.enumeration.AppHttpCodeEnum;
import com.weizai.carshop.common.result.ResponseResult;
import com.weizai.carshop.model.dto.ShopClassDto;
import com.weizai.carshop.model.entiity.ShopCateInfoEntity;
import com.weizai.carshop.model.entiity.ShopClassEntity;
import com.weizai.carshop.service.ShopClassService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/admin/shopClass")
@RequiredArgsConstructor
public class ShopClassController {
    @Autowired
    private ShopClassService shopClassService;
    /**
     * 获取栏目列表
     */
    @GetMapping("/list")
    public ResponseResult list() {
        List<ShopClassEntity> list = shopClassService.list();
        return ResponseResult.okResult(list);
    }

    /**
     * 根据id删除栏目
     */
    @DeleteMapping("/del/{id}")
    public ResponseResult delCateById(@PathVariable Long id) {
        shopClassService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 新增或修改栏目
     */
    @PostMapping("/setShopClass")
    public ResponseResult setShopClass(@RequestBody ShopClassDto shopClassDto) {
        ShopClassEntity shopClassEntity = BeanUtil.copyProperties(shopClassDto, ShopClassEntity.class);
        shopClassService.saveOrUpdate(shopClassEntity);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
